// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
import androidx.compose.animation.core.*
import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.window.WindowDraggableArea
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.paint
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.*
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowState
import androidx.compose.ui.window.application
import java.time.LocalTime

@Composable
@Preview
fun App() {

    // 定义初始动画
    val infiniteTransaction = rememberInfiniteTransition()

    val localTime = LocalTime.now()

    // 秒针角度定义
    // 设置初始值
    val secondAngle by infiniteTransaction.animateFloat(
        initialValue = 0f,
        targetValue = 360f,
        animationSpec = infiniteRepeatable(
            animation = tween(60000, easing = LinearEasing)
        )
    )

    // 分针角度定义
    // 设置初始值
    val minuteAngle by infiniteTransaction.animateFloat(
        initialValue = 0f,
        targetValue = 360f,
        animationSpec = infiniteRepeatable(
            animation = tween(3600000, easing = LinearEasing),

        )
    )

    // 时针角度定义
    // 设置初始值
    val hourAngle by infiniteTransaction.animateFloat(
        initialValue = 0f,
        targetValue = 360f,
        animationSpec = infiniteRepeatable(
            animation = tween(3600000*12, easing = LinearEasing),

            )
    )

    Row(modifier = Modifier.size(300.dp).fillMaxHeight().paint(
        painter = painterResource("sdh.png"),
        alpha = 0.5f,
        contentScale = ContentScale.Inside
    ),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceAround,
    ) {
        Canvas(modifier = Modifier.size(200.dp, 200.dp)
            ,
            onDraw = {
                // Head
                drawCircle(
                    Brush.linearGradient(
                        colors = listOf(Color.Red, Color.Green)
                    ),
                    radius = size.width / 2,
                    center = center,
                    style = Stroke(width = 5.0f)
                )

                // 画一个中心圆点
                drawCircle(
                    color = Color.Black,
                    radius = 4.0f,
                    center = center
                )

                rotate(degrees = secondAngle) {

                    // 弧度值
                    val rad = Math.PI * 2 / 360 * localTime.second.toDouble() * 6
                    val length = (size.width / 2 - size.width * 0.075f * 0.5)

                    // 画秒针
                    drawLine(
                        color = Color.Black,
                        start = center,
                        end = center + Offset((Math.sin(rad) * length).toFloat(), (-Math.cos(rad) * length).toFloat()),
                        strokeWidth = 2.0f
                    )
                }

                rotate(degrees = minuteAngle) {

                    // 弧度值
                    val rad = Math.PI * 2 / 360 * (localTime.minute.toDouble() + localTime.second.toDouble() / 60) * 6
                    val length = size.width / 2 * 0.75f
                    // 画分针
                    drawLine(
                        color = Color.Black,
                        start = center,
                        end = center + Offset((Math.sin(rad) * length).toFloat(), (-Math.cos(rad) * length).toFloat()),
                        strokeWidth = 2.0f,
                    )
                }

                rotate(degrees = hourAngle) {

                    // 弧度值
                    val rad = Math.PI * 2 / 360 * (localTime.hour.toDouble() + localTime.minute.toDouble() / 60 + localTime.second.toDouble() / 3600) * 30
                    val length = size.width / 2 * 0.5f
                    // 画时针
                    drawLine(
                        color = Color.Black,
                        start = center,
                        end = center + Offset((Math.sin(rad) * length).toFloat(), (-Math.cos(rad) * length).toFloat()),
                        strokeWidth = 3.0f,
                    )
                }

                // 画数字
                for (i in 1..12) {
                    drawIntoCanvas {
                        // 弧度值
                        val rad = Math.PI * 2 / 12 * i
                        val length = size.width / 2 * 0.9f

                        it.nativeCanvas.drawString(
                            s = i.toString(),
                            x = center.x - 2 + (Math.sin(rad) * length).toFloat(),
                            y = center.y + 2 + (-Math.cos(rad) * length).toFloat(),
                            paint = org.jetbrains.skia.Paint().apply {
                                color = Color.Black.toArgb()
                            },
                            font = org.jetbrains.skia.Font(
                                typeface = org.jetbrains.skia.Typeface.makeDefault(),
                                size = 10.0f
                            )
                        )
                    }
                }

                //
            }
        )
    }

}

fun main() = application {
    Window(onCloseRequest = ::exitApplication,
        state = WindowState(width = 300.dp, height = 300.dp),
        resizable = false,
        title = "时钟",
        icon = painterResource("time.png"),
        undecorated = true,
        transparent = true
    ) {
        WindowDraggableArea {
            App()
        }
    }
}
